/**
 * ace.css 专用js
 */
function inits(){
	var winHeight = $(window).height();
	$("#top,#bottom").width($(window).width()*0.8);
	$("#right>.top,#right>.center").height((winHeight-281)*0.5);
	$("#top").height(winHeight*0.5);
	$("#bottom").height(winHeight*0.5);
	var boxHeight = $("#bottom .right .box").height()-45;
	$("#bottom .right .box .top").height(boxHeight-255);
	var boxs = $("#bottom .right .box .top .content-boxs");
	boxs.height(boxs.parent().height()-40);
	$(".box").each(function(i,item){
		$(this).find(".content-box").height($(this).height()-45);
	});
	if($("#bottom .right .box .top").height() <= 70){
		$("#bottom .right .box .top").hide();
	}else{
		$("#bottom .right .box .top").show();
	}
}
$(window).on("load resize",function(){
	inits()
});

/**
 * @description 延时加载相关数据.<br>
 * @athor zcg .<br>
 * @date 2018/7/25 .<br>
 */
setTimeout(function(){
    //公告通知
    loadNotice();
    //待办事项
    loadWorkFlow();
    
},800);

/**
 * @description 加载流程待办事项.<br>
 * @athor zcg .<br>
 * @date 2018/7/25 .<br>
 */
function loadWorkFlow(){
	var backlogData = '';
    $.ajax({
    	type:"post",
    	url: _baseURL + "/iface/web/home/findMyTaskList",
    	async:false,
    	success:function(res){
    		$.each(res,function(i,item){
    			var noticeTemplet = item.noticeTemplet;
    			if(noticeTemplet){
    				var billInfo = item.billInfo;
    				$.each(billInfo,function(key,value){
    					if(noticeTemplet.indexOf(key) != -1 ){
    						noticeTemplet = noticeTemplet.replace("${"+key+"}",value);
    					}
    				});
    			}else{
    				noticeTemplet = item.name;
    			}
    			var a ='<span style="background-color:'
					+(item.treatmentType==0?"#FF6100":"#1296DB")+'">'
					+(item.treatmentType==0?"待办":"待签收")+'</span>';
    			var b = '';
    			if(item.business){
    				b = "<label style='font-weight:bold;color:green'>["+item.business+"]</label>";
    			}
    			backlogData += `<li><a href="javascript:" class="task_href" title="${noticeTemplet}" data-href="${_baseURL}/admin/${item.url}">${a}
    				${b} 请您处理<font color="#0C98F5">${noticeTemplet	}</font>流程</a></li>`;
    		});
    	},
    	complete:function(res){
    		$("#top-right").css({"height":$("#top-right").parents(".top").height()-60+"px","overflow-y":"auto"});
    		$("#top-right").html(backlogData);
    		$("a.task_href").on("click",function(){
    			var urls = $(this).data("href");
    			//alert(urls)
    			auditeFlow(urls);
    		});
    	},
    	error:function(){
    		$("#top-right").html('<font color="red">暂无数据!</font>');
    	}
    });
}

function auditeFlow(urls){
	top.layer.open({
		  type: 2,
		  title: "审批信息",
		  shadeClose: true,
		  skin: 'layui-layer-rim', //加上边框
		  shade: 0.5,
		  area: ['98%', '98%'],
		  content: urls,
		  canCallBack:function(){
		  },
		  cancel:function(index, layero){
		  },
		  end: function () {
			  loadMyTask();
		  }
	});
}

/**
 * @description 加载通知.<br>
 * @athor zcg .<br>
 * @date 2018/7/25 .<br>
 */
function loadNotice(){
	var noticeData = '';
    $.ajax({
    	type:"post",
    	url: _baseURL + "/iface/web/home/findNotifyList",
    	async:false,
    	success:function(res){
    		$.each(res,function(i,item){
    			noticeData += '<li><a title="'+item.title+'" href="javascript:"><span>'+item.createDate+'</span>'+item.title+'</a></li>';
						//+'<div class="ctent" title="'+item.content+'">'+item.content+'</div></li>';
    		});
    	},
    	complete:function(res){
    		$("#scrollDiv ul").html(noticeData);
    		//初始化“公告通知”滚动
    		$("#scrollDiv").Scroll({line:1,speed:500,timer:3000,up:"but_up",down:"but_down"});
    	}
    });
}

$(function(){
    //页面加载初始化年月
    var mydate = new Date();
    $(".f-year").html( mydate.getFullYear() );
    $(".f-month").html( mydate.getMonth()+1 );
    showDate(mydate.getFullYear(),mydate.getMonth()+1);
    //日历上一月
    $(".f-btn-jian ").click(function(){
        var mm = parseInt($(".f-month").html());
        var yy = parseInt($(".f-year").html());
        if( mm == 1){//返回12月
            $(".f-year").html(yy-1);
            $(".f-month").html(12);
            showDate(yy-1,12);
        }else{//上一月
            $(".f-month").html(mm-1);
            showDate(yy,mm-1);
        }
    })
    //日历下一月
    $(".f-btn-jia").click(function(){
        var mm = parseInt($(".f-month").html());
        var yy = parseInt($(".f-year").html());
        if( mm == 12){//返回12月
            $(".f-year").html(yy+1);
            $(".f-month").html(1);
            showDate(yy+1,1);
        }else{//上一月
            $(".f-month").html(mm+1);
            showDate(yy,mm+1);
        }
    })
    //返回本月
    $(".f-btn-fhby").click(function(){
        $(".f-year").html( mydate.getFullYear() );
        $(".f-month").html( mydate.getMonth()+1 );
        showDate(mydate.getFullYear(),mydate.getMonth()+1);
    })
    
    //读取年月写入日历
    function showDate(yyyy,mm){
        var dd = new Date(parseInt(yyyy),parseInt(mm), 0);   //Wed Mar 31 00:00:00 UTC+0800 2010  
        var daysCount = dd.getDate();            //本月天数  
        var mystr ="";//写入代码
        var today = new Date().getDate(); //今天几号  21
        var monthstart = new Date(parseInt(yyyy)+"/"+parseInt(mm)+"/1").getDay()//本月1日周几  
        var lastMonth; //上一月天数
        var nextMounth//下一月天数
        if(  parseInt(mm) ==1 ){
            lastMonth = new Date(parseInt(yyyy)-1,parseInt(12), 0).getDate();
        }else{
            lastMonth = new Date(parseInt(yyyy),parseInt(mm)-1, 0).getDate();
        }
        if( parseInt(mm) ==12 ){
            nextMounth = new Date(parseInt(yyyy)+1,parseInt(1), 0).getDate();
        }else{
            nextMounth = new Date(parseInt(yyyy),parseInt(mm)+1, 0).getDate();
        }
        //计算上月空格数
        for(j=monthstart;j>0;j--){
            mystr += "<div class='f-td f-null f-lastMonth' style='color:#ccc;'>"+(lastMonth-j+1)+"</div>";
        }
        //本月单元格
        for(i=0;i<daysCount;i++){
             //这里为一个单元格，添加内容在此
            if(i<5){ 
            	mystr += "<div class='f-td f-number'><span class='f-day'>"+(i+1)+"</span>"
                    +"<div class='f-yuan'></div>"
                   // +"<div class='f-table-msg'>回款中<span class='major'>"+(i+1)+"</span>笔。回款本息;<span class='major'>1，000，000</span>元</div>"//这里加判断
                    +"</div>"; 
            }else{
            	mystr += "<div class='f-td f-number'><span class='f-day'>"+(i+1)+"</span></div>";
            }
        }
        
        //计算下月空格数
        for(k=0; k<42-(daysCount+monthstart);k++ ){//表格保持等高6行42个单元格
            mystr += "<div class='f-td f-null f-nextMounth' style='color:#ccc;'>"+(k+1)+"</div>";
        }
         
        //写入日历
        $(".f-rili-table .f-tbody").html(mystr);
        //给今日加class
        if( mydate.getFullYear() == yyyy){
            if( (mydate.getMonth()+1 ) == mm){
                var today = mydate.getDate();
                var lastNum = $(".f-lastMonth").length;
                $(".f-rili-table .f-td").eq(today+lastNum-1).addClass("f-today");
            }
        }
        //绑定选择方法
        $(".f-rili-table .f-number").off("click");
        $(".f-rili-table .f-number").on("click",function(){
            $(".f-rili-table .f-number").removeClass("f-on");
            $(this).addClass("f-on");
        });
        
        //绑定查看方法
        $(".f-yuan").off("mouseover");
        $(".f-yuan").on("mouseover",function(){
            $(this).parent().find(".f-table-msg").show();
        });
        $(".f-table-msg").off("mouseover");
        $(".f-table-msg").on("mouseover",function(){
            $(this).show();
        });
        $(".f-yuan").off("mouseleave");
        $(".f-yuan").on("mouseleave",function(){
            $(this).parent().find(".f-table-msg").hide();
        });
        $(".f-table-msg").off("mouseleave");
        $(".f-table-msg").on("mouseleave",function(){
            $(this).hide();
        });
    }
   
});
